/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.channel.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.wechat.service.WeixinService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.channel.vo.ChannelVO;
import com.fanrenqiji.modules.business.channel.service.ChannelService;

@Controller
@RequestMapping(value = "${adminPath}/business/channel")
public class ChannelController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(ChannelController.class);
    @Autowired
    private ChannelService channelService;
    @Autowired
    private WeixinService wxService;

    @ModelAttribute("channel")
    public ChannelVO getChannel(@RequestParam(required = false) String id) {
        if (StringUtils.isNotEmpty(id)) {
            return channelService.get(id);
        } else {
            return new ChannelVO();
        }
    }

    /**
     * 列表
     *
     * @return
     */
    @RequiresPermissions("business:channel:view")
    @RequestMapping(value = {"list", ""})
    public String list(ChannelVO channel, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
        Page<ChannelVO> page = channelService.findChannelByPage(new Page<ChannelVO>(request, response),
                channel);
        model.addAttribute("page", page);
        model.addAttribute("channel", channel);
        return "modules/business/channel/channelList";
    }

    /**
     * 选择列表
     *
     * @return
     */
    @RequestMapping(value = "select")
    public String select(ChannelVO channel, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ChannelVO> page = channelService.findChannelByPage(new Page<ChannelVO>(request, response, 10), channel);
        model.addAttribute("page", page);
        model.addAttribute("channel", channel);
        return "modules/business/channel/channelSelect";
    }

    /**
     * 用户添加更新表
     *
     * @param model
     * @return
     */
    @RequiresPermissions("business:channel:edit")
    @RequestMapping(value = "form")
    public String form(Model model, ChannelVO channel) {
        if (StringUtils.isNotEmpty(channel.getId())) {
            model.addAttribute("channel", channelService.get(channel.getId()));
        } else {
            model.addAttribute("channel", channel);
        }
        return "modules/business/channel/channelForm";
    }

    /**
     * 添加或修改提交
     *
     * @param channel
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("business:channel:edit")
    @RequestMapping(value = "save")
    public String save(ChannelVO channel, Model model,
                       RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
        if (!beanValidator(model, channel)) {
            return form(model, channel);
        }

        try {
            WxMpQrCodeTicket ticket = wxService.getQrcodeService().qrCodeCreateLastTicket("fanrenqiji_wx_parent_2_");
            String url = wxService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());
            channel.setQrCode(url);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        channelService.update(channel);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/channel/?repage";
    }

    /**
     * 删除操作
     *
     * @return
     */
    @RequestMapping("delete")
    @RequiresPermissions("business:channel:delete")
    public String delete(String id, RedirectAttributes redirectAttributes) {
        channelService.delete(id);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/channel/";
    }
}